{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-04T18:38:12.552928Z",
     "start_time": "2017-10-04T18:38:12.543725Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3.0\n",
      "Hello!\n",
      "Wonderful!\n"
     ]
    }
   ],
   "source": [
    "x = 3 # 整数\n",
    "y = 3. # 浮点数\n",
    "z = \"Hello!\" # 字符串\n",
    "Z = \"Wonderful!\" # 另一个字符串，用大写的Z表示\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)\n",
    "print(Z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:38:17.443096Z",
     "start_time": "2018-10-09T01:38:17.439036Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.0\n"
     ]
    }
   ],
   "source": [
    "sum_ = x + y # 整数+浮点数 = 浮点数，因为浮点数更大\n",
    "print(sum_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-04T18:38:13.972732Z",
     "start_time": "2017-10-04T18:38:13.967236Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello! World!\n"
     ]
    }
   ],
   "source": [
    "v = \"World!\"\n",
    "sum_string = z + \" \" + v # 字符串拼接\n",
    "print(sum_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用 `%` 进行格式化打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-04T18:38:15.178277Z",
     "start_time": "2017-10-04T18:38:15.173368Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sum of x and y is 6.00\n"
     ]
    }
   ],
   "source": [
    "print(\"The sum of x and y is %.2f\"%sum_) # 用 %f 表示浮点数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-04T18:38:15.472965Z",
     "start_time": "2017-10-04T18:38:15.467306Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The string `sum_string` is 'Hello! World!'\n"
     ]
    }
   ],
   "source": [
    "print(\"The string `sum_string` is '%s'\"%sum_string) # 用 %s 表示字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 命名格式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two commonly used style in programming:\n",
    "\n",
    "1. __camelCase__\n",
    "2. __snake_case__ or __lower_case_with_underscore__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All variable (function and class) names must start with a letter or underscore (\\_). You can include numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:28.497486Z",
     "start_time": "2018-10-09T00:16:28.491966Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'my string'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myStringHere = 'my string'\n",
    "myStringHere"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:28.879472Z",
     "start_time": "2018-10-09T00:16:28.875812Z"
    }
   },
   "outputs": [],
   "source": [
    "x = 3 # valid\n",
    "x_3 = \"xyz\" # valid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:29.096887Z",
     "start_time": "2018-10-09T00:16:29.091476Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid token (<ipython-input-59-520aa7218b05>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-59-520aa7218b05>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    3_x = \"456\" # invalid. Numbers cannot be in the first position.\u001b[0m\n\u001b[0m     ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid token\n"
     ]
    }
   ],
   "source": [
    "3_x = \"456\" # invalid. Numbers cannot be in the first position."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can choose either camel case or snake case. Always make sure you use one convention consistenly across one project."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To initialize a string variable, you can use either double or single quotes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:32.520585Z",
     "start_time": "2018-10-09T00:16:32.516838Z"
    }
   },
   "outputs": [],
   "source": [
    "store_name = \"HyVee\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can think of strings as a sequence of characters (or a __list__ of characters, see the next section). In this case, indices and bracket notations can be used to access specific ranges of characters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:32.814921Z",
     "start_time": "2018-10-09T00:16:32.811324Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yVe\n"
     ]
    }
   ],
   "source": [
    "name_13 = store_name[1:4] # [start, end), end is exclusive; Python starts with 0 NOT 1\n",
    "print(name_13)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:33.080003Z",
     "start_time": "2018-10-09T00:16:33.075926Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e\n"
     ]
    }
   ],
   "source": [
    "last_letter = store_name[-1] # -1 means the last element\n",
    "print(last_letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 简单数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For numbers w/o fractional parts, we say they are ___integer___. In Python, they are called `int`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:51:10.632494Z",
     "start_time": "2018-10-08T23:51:10.598421Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 3\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For numbers w/ fractional parts, they are floating point numbers. They are named `float` in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:51:54.013974Z",
     "start_time": "2018-10-08T23:51:54.008407Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = 3.0\n",
    "type(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:55:03.134594Z",
     "start_time": "2018-10-08T23:55:03.118103Z"
    }
   },
   "source": [
    "We can apply arithmetic to these numbers. However, one thing we need to be careful about is ___type conversion___. See the example below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:55:31.855870Z",
     "start_time": "2018-10-08T23:55:31.847879Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = 2 * x\n",
    "type(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:55:36.207707Z",
     "start_time": "2018-10-08T23:55:36.202449Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = y + x\n",
    "type(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 文本/字符/字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Python, we use `str` type for storing letters, words, and any other characters, as mentioned previously in Section 2.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:57:26.831211Z",
     "start_time": "2018-10-08T23:57:26.826020Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_word = \"see you\"\n",
    "type(my_word)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unlike numbers, `str` is an iterable object, meaning that we can iterate through each individual character:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:58:44.058368Z",
     "start_time": "2018-10-08T23:58:44.052694Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('s', 'e yo')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_word[0], my_word[2:6]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use `+` to _concatenate_ different strings "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-08T23:59:16.399980Z",
     "start_time": "2018-10-08T23:59:16.395030Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'see you tomorrow'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_word + ' tomorrow'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 布尔类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Boolean type comes in handy when we need to check conditions. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:00:33.751846Z",
     "start_time": "2018-10-09T00:00:33.746658Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, bool)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_error = 1.6\n",
    "compare_result = my_error < 0.1\n",
    "compare_result, type(compare_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two and only two valid Boolean values: `True` and `False`. We can also think of them as `1` and `0`, respectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:02:30.834630Z",
     "start_time": "2018-10-09T00:02:30.830339Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_error > 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we use Boolean values for arithmetic operations, they will become `1/0` automatically"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:02:52.791120Z",
     "start_time": "2018-10-09T00:02:52.783315Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(my_error>0) + 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 类型转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since variables in Python are dynamically typed, we need to be careful about type conversion."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When two variables share the same data type, there is not much to be worried about:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:04:59.077234Z",
     "start_time": "2018-10-09T00:04:59.072252Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'no problem. talk to you later'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = \"no problem. \"\n",
    "s2 = \"talk to you later\"\n",
    "s1 + s2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But be careful when we are mixing variables up:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:06:03.221806Z",
     "start_time": "2018-10-09T00:06:03.217855Z"
    }
   },
   "outputs": [],
   "source": [
    "a = 3 # recall that this is an ____?\n",
    "b = 2.7 # how about this?\n",
    "c = a + b # what is the type of `c`?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To make things work between string and numbers, we can explicitly convert numbers into `str`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:07:59.974061Z",
     "start_time": "2018-10-09T00:07:59.964373Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m----------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)",
      "\u001b[0;32m<ipython-input-26-f90ed9df3b77>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "s1 + 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:08:04.034457Z",
     "start_time": "2018-10-09T00:08:04.027842Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'no problem. 3'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 + str(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 复杂数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### List"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize a list with brackets. You can store anything in a list, even if they are different types\n",
    "- note that we use [___string formatting___](https://pyformat.info/) to display strings\n",
    "- `%i` is a placeholder for `int`\n",
    "- `%s` for `str`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:24.538230Z",
     "start_time": "2018-10-09T00:10:24.528793Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of a_list is: 3\n",
      "The 3rd element of a_list is: 3\n",
      "The last element of a_list is: 3\n",
      "The sum of a_list is 6.00\n"
     ]
    }
   ],
   "source": [
    "a_list = [1, 2, 3] # commas to seperate elements\n",
    "print(\"Length of a_list is: %i\"%(len(a_list)))\n",
    "print(\"The 3rd element of a_list is: %s\" %(a_list[2])) # Remember Python starts with 0\n",
    "print(\"The last element of a_list is: %s\" %(a_list[-1])) # -1 means the end\n",
    "print(\"The sum of a_list is %.2f\"%(sum(a_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:32.820098Z",
     "start_time": "2018-10-09T00:10:32.815341Z"
    }
   },
   "source": [
    "We can put different types in a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:35.486742Z",
     "start_time": "2018-10-09T00:10:35.479281Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, True, 'good', 'good']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b_list = [20, True, \"good\", \"good\"] \n",
    "b_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Update a list: __pop__, __remove__, __append__, __extend__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:41.107486Z",
     "start_time": "2018-10-09T00:10:41.102493Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "Pop 2 out of a_list\n",
      "[1, 3]\n"
     ]
    }
   ],
   "source": [
    "print(a_list)\n",
    "print(\"Pop %i out of a_list\"%a_list.pop(1)) # pop the value of an index\n",
    "print(a_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:43.906572Z",
     "start_time": "2018-10-09T00:10:43.899877Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Remove the string good from b_list:\n",
      "[20, True, 'good']\n"
     ]
    }
   ],
   "source": [
    "print(\"Remove the string good from b_list:\")\n",
    "b_list.remove(\"good\") # remove a specific value (the first one in the list)\n",
    "print(b_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:45.307563Z",
     "start_time": "2018-10-09T00:10:45.302951Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After appending a new value, a_list is now: [1, 3, 10]\n"
     ]
    }
   ],
   "source": [
    "a_list.append(10)\n",
    "print(\"After appending a new value, a_list is now: %s\"%(str(a_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:45.952388Z",
     "start_time": "2018-10-09T00:10:45.943473Z"
    }
   },
   "source": [
    "merge `a_list` and `b_list`: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:02.249064Z",
     "start_time": "2018-10-09T00:12:02.241901Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Merging a_list and b_list: [1, 3, 10, 20, True, 'good', 20, True, 'good']\n"
     ]
    }
   ],
   "source": [
    "a_list.extend(b_list)\n",
    "print(\"Merging a_list and b_list: %s\"%(str(a_list)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:10:45.952388Z",
     "start_time": "2018-10-09T00:10:45.943473Z"
    }
   },
   "source": [
    "We can also use `+` to concatenate two lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:03.301969Z",
     "start_time": "2018-10-09T00:12:03.295626Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 10, 20, True, 'good', 20, True, 'good', 20, True, 'good']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_list + b_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tuple (A special case of list whose elements cannot be changed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-03T15:58:48.469747Z",
     "start_time": "2017-10-03T15:58:48.461327Z"
    }
   },
   "source": [
    "Initialize a tuple with paranthesis. The major difference between list and tuple is that you can alter list but not tuple."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:16.790034Z",
     "start_time": "2018-10-09T00:12:16.784149Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 10)\n",
      "First element of a_tuple: 1\n"
     ]
    }
   ],
   "source": [
    "a_tuple = (1, 2, 3, 10)\n",
    "print(a_tuple)\n",
    "print(\"First element of a_tuple: %i\"%a_tuple[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:17.683012Z",
     "start_time": "2018-10-09T00:12:17.672651Z"
    }
   },
   "source": [
    "You cannot change the values of a_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:24.288097Z",
     "start_time": "2018-10-09T00:12:24.278444Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m----------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)",
      "\u001b[0;32m<ipython-input-45-0775dda10b17>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma_tuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "a_tuple[0] = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to create a single value tuple, you need to add a ','"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "a_tuple = (1) # this would create a int type\n",
    "print(type(a_tuple))\n",
    "b_tuple = (1,) # this would create a tuple type, take note of the comma.\n",
    "print(type(b_tuple))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Dictionary: key-value pairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize a dict by curly brackets `{}`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:58.821876Z",
     "start_time": "2018-10-09T00:12:58.817697Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: '1 value'}\n"
     ]
    }
   ],
   "source": [
    "d = {} # empty dictionary\n",
    "d[1] = \"1 value\" # add a key-value by using bracket (key). You can put anything in key/value.\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:12:59.006654Z",
     "start_time": "2018-10-09T00:12:59.002247Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: '1 value', 2: '2 value', 3: '3 value', 4: '4 value', 5: '5 value', 6: '6 value', 7: '7 value', 8: '8 value', 9: '9 value'}\n",
      "All the keys: dict_keys([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
      "All the values: dict_values(['1 value', '2 value', '3 value', '4 value', '5 value', '6 value', '7 value', '8 value', '9 value'])\n"
     ]
    }
   ],
   "source": [
    "# Use for loop to add values\n",
    "for index in range(2, 10):\n",
    "    d[index] = \"%i value\"%index\n",
    "print(d)\n",
    "print(\"All the keys: \" + str(d.keys()))\n",
    "print(\"All the values: \" + str(d.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:13:11.439289Z",
     "start_time": "2018-10-09T00:13:11.434000Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Key is: 1, Value is : 1 value\n",
      "Key is: 2, Value is : 2 value\n",
      "Key is: 3, Value is : 3 value\n",
      "Key is: 4, Value is : 4 value\n",
      "Key is: 5, Value is : 5 value\n",
      "Key is: 6, Value is : 6 value\n",
      "Key is: 7, Value is : 7 value\n",
      "Key is: 8, Value is : 8 value\n",
      "Key is: 9, Value is : 9 value\n"
     ]
    }
   ],
   "source": [
    "for key in d:\n",
    "    print(\"Key is: %i, Value is : %s\"%(key, d[key]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 控制逻辑\n",
    "\n",
    "In the following examples, we show examples of comparison, `if-else` loop, `for` loop, and `while` loop."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comparison"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python syntax for comparison is the same as our hand-written convention: \n",
    "\n",
    "1. Larger (or equal): `>` (`>=`)\n",
    "2. Smaller (or equal): `<` (`<=`)\n",
    "3. Equal to: `==` (__Notie here that there are double equal signs__)\n",
    "4. Not equal to: `!=`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:20.683160Z",
     "start_time": "2018-10-09T00:16:20.676171Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 == 5 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:21.107023Z",
     "start_time": "2018-10-09T00:16:21.102607Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "72 >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:49.169393Z",
     "start_time": "2018-10-09T00:16:49.164386Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HyVee'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "store_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:16:49.460268Z",
     "start_time": "2018-10-09T00:16:49.453590Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "store_name == \"HyVee\" # Will return a boolean value True or False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "IMPORTANT: It is worth noting that comparisons between floating point numbers are tricky."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:17:16.577984Z",
     "start_time": "2018-10-09T00:17:16.571556Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.6000000000000005\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(2.2 * 3.0)\n",
    "2.2 * 3.0 == 6.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:17:16.777940Z",
     "start_time": "2018-10-09T00:17:16.771796Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3.3 * 2.0 == 6.6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From @Amandan from [Stack Overflow](https://stackoverflow.com/users/3346210/zhiya?tab=questions&sort=newest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T00:17:17.392986Z",
     "start_time": "2018-10-09T00:17:17.388888Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0x1.a666666666667p+2\n",
      "0x1.a666666666666p+2\n",
      "0x1.a666666666666p+2\n"
     ]
    }
   ],
   "source": [
    "print(float.hex(2.2 * 3.0))\n",
    "print(float.hex(3.3 * 2.0))\n",
    "print(float.hex(6.6))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Therefore, be really careful when you have to do such comparison"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### If-Else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:38:46.027739Z",
     "start_time": "2018-10-09T01:38:46.023274Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:38:47.709640Z",
     "start_time": "2018-10-09T01:38:47.704307Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum_ is above 0 and its value is 6.0\n"
     ]
    }
   ],
   "source": [
    "if sum_ == 0:\n",
    "    print(\"sum_ is 0\")\n",
    "elif sum_ < 0:\n",
    "    print(\"sum_ is less than 0\")\n",
    "else:\n",
    "    print(\"sum_ is above 0 and its value is \" + str(sum_)) # Cast sum_ into string type."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that you do not have to use `if-else` or `if-elif-...-else`. You can use `if` without other clauses following that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:38:49.218876Z",
     "start_time": "2018-10-09T01:38:49.214871Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum_ is above 5\n"
     ]
    }
   ],
   "source": [
    "if sum_ > 5:\n",
    "    print('sum_ is above 5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparing strings are similar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:38:50.755605Z",
     "start_time": "2018-10-09T01:38:50.751523Z"
    }
   },
   "outputs": [],
   "source": [
    "store_name = 'Walmart'\n",
    "#store_name = 'Hyvee'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:38:57.568568Z",
     "start_time": "2018-10-09T01:38:57.563726Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The store is not Walmart. It's Walmart.\n"
     ]
    }
   ],
   "source": [
    "if 'Wal' in store_name:\n",
    "    print(\"The store is not Walmart. It's \" + store_name + \".\")\n",
    "else:\n",
    "    print(\"The store is Walmart.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### For loop: Iterating thru a sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:39:07.309307Z",
     "start_time": "2018-10-09T01:39:07.305696Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W\n",
      "a\n",
      "l\n",
      "m\n",
      "a\n",
      "r\n",
      "t\n"
     ]
    }
   ],
   "source": [
    "for letter in store_name:\n",
    "    print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-04T18:38:51.792543Z",
     "start_time": "2017-10-04T18:38:51.786228Z"
    }
   },
   "source": [
    "`range()` is a function to create interger sequences:\n",
    "- Note that Python 3 now returns an `iterator` instead of actual `list` with `range` function\n",
    "- See [link](https://stackoverflow.com/questions/44571718/python-3-range-vs-python-2-range)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:03.662280Z",
     "start_time": "2018-10-09T01:41:03.657790Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(5) gives[0, 1, 2, 3, 4]\n",
      "range(1,9) gives: [1, 2, 3, 4, 5, 6, 7, 8]\n"
     ]
    }
   ],
   "source": [
    "print(\"range(5) gives\" + str(list(range(5)))) # By default starts from 0\n",
    "print(\"range(1,9) gives: \" + str(list(range(1, 9)))) # From 1 to 9-1 (Again the end index is exclusive.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:05.935416Z",
     "start_time": "2018-10-09T01:41:05.930030Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The 0th letter in store_name is: W\n",
      "The 1th letter in store_name is: a\n",
      "The 2th letter in store_name is: l\n",
      "The 3th letter in store_name is: m\n",
      "The 4th letter in store_name is: a\n",
      "The 5th letter in store_name is: r\n",
      "The 6th letter in store_name is: t\n"
     ]
    }
   ],
   "source": [
    "for index in range(len(store_name)): # length of a sequence\n",
    "    print(\"The %ith letter in store_name is: %s\"%(index, store_name[index]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### While loop: Keep doing until condition no longer holds."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use `for` when you know __the exact number of iterations__; use `while` when you __do not (e.g., checking convergence)__."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:12.053207Z",
     "start_time": "2018-10-09T01:41:12.049347Z"
    }
   },
   "outputs": [],
   "source": [
    "x = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:12.294825Z",
     "start_time": "2018-10-09T01:41:12.290846Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "5\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "while x < 10:\n",
    "    print(x)\n",
    "    x = x + (x-1)\n",
    "    #x += x-1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Notes on `break` and `continue`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`break` means get out of the loop immediately. Any code after the `break` will NOT be executed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:21.493159Z",
     "start_time": "2018-10-09T01:41:21.489747Z"
    }
   },
   "outputs": [],
   "source": [
    "store_name = 'Walmart'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:22.084338Z",
     "start_time": "2018-10-09T01:41:22.076857Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W\n",
      "End at a\n"
     ]
    }
   ],
   "source": [
    "index = 0\n",
    "while True:\n",
    "    print(store_name[index])\n",
    "    index += 1 # a += b means a = a + b\n",
    "    if store_name[index] == \"a\":\n",
    "        print(\"End at a\")\n",
    "        break # instead of setting flag to False, we can directly break out of the loop\n",
    "        print(\"Hello!\") # This will NOT be run"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`continue` means get to the next iteration of loop. It will __break__ the current iteration and __continue__ to the next."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:23.822603Z",
     "start_time": "2018-10-09T01:41:23.818257Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W\n",
      "l\n",
      "m\n",
      "r\n",
      "t\n"
     ]
    }
   ],
   "source": [
    "for letter in store_name:\n",
    "    if letter == \"a\":\n",
    "        continue # Not printing V\n",
    "    else:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:41:24.791324Z",
     "start_time": "2018-10-09T01:41:24.780078Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W\n",
      "Hello!\n",
      "a\n",
      "This is an `a`\n",
      "l\n",
      "Hello!\n",
      "m\n",
      "Hello!\n",
      "a\n",
      "This is an `a`\n",
      "r\n",
      "Hello!\n",
      "t\n",
      "Hello!\n"
     ]
    }
   ],
   "source": [
    "index = 0\n",
    "while index <= len(store_name)-1:\n",
    "    print(store_name[index])\n",
    "    if store_name[index] == \"a\":\n",
    "        print(\"This is an `a`\")\n",
    "        index += 1 # a += b means a = a + b\n",
    "        continue\n",
    "    print(\"Hello!\") # This will NOT be run\n",
    "    index += 1 # a += b means a = a + b"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "677px",
    "left": "0px",
    "right": "1111px",
    "top": "43px",
    "width": "340px"
   },
   "toc_section_display": "block",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
